package sasp_messages;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 *
 * @author j
 */
public class SASP_RSA {

    	private PrivateKey privKey;
        private PublicKey pubKey;
        private String filePath = "/home/j/Code/keys/";
        private int keySize = 1024;

        /*
         * Constructing and saving/loading our RSA keys
         * Modified save method from: http://www.javamex.com/tutorials/cryptography/rsa_encryption.shtml
         * http://snipplr.com/view/18368/saveload--private-and-public-key-tofrom-a-file/
         */

        public void setPath(String _path)   {
            filePath = _path;
        }

        private byte[] loadRSA(String filePathName) throws IOException, ClassNotFoundException  {
            File keyFile = new File(filePathName);
            FileInputStream fis = new FileInputStream(filePathName);
            byte[] encodedKey = new byte[(int) keyFile.length()];
            fis.read(encodedKey);
            fis.close();
            return encodedKey;
        }

        public PublicKey loadPub(String fileName) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException   {

            byte[] _publicKey = loadRSA(filePath+fileName+".pub");
            X509EncodedKeySpec _keySpec = new X509EncodedKeySpec(_publicKey);
            KeyFactory factory = KeyFactory.getInstance("RSA");
            return factory.generatePublic(_keySpec);
        }

        public PrivateKey loadPriv(String fileName) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, ClassNotFoundException {

            byte[] _privateKey = loadRSA(filePath+fileName+".priv");
            PKCS8EncodedKeySpec _keySpec = new PKCS8EncodedKeySpec(_privateKey);
            KeyFactory factory = KeyFactory.getInstance("RSA");
            return factory.generatePrivate(_keySpec);
        }

        /*
         * Only used to generate an RSA key-- generally these are supplied, and
         * makeRSA() unused otherwise.
         */
        public void makeRSA() throws NoSuchAlgorithmException, InvalidKeySpecException{
             KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
             kpg.initialize(keySize);
             KeyPair rsaKeys = kpg.generateKeyPair();
             pubKey = rsaKeys.getPublic();
             privKey = rsaKeys.getPrivate();
         }

        /*
         * When generating an RSA key, saveRSA saves the keys as bytes to a file.
         * Not used when keys are supplied.
         */
        public void saveRSA(String fileName) throws IOException {
           try
            {
                X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey.getEncoded());
                FileOutputStream fosPub = new FileOutputStream(filePath+fileName+".pub");
                byte[] someKey = x509EncodedKeySpec.getEncoded();
                fosPub.write(x509EncodedKeySpec.getEncoded());
                fosPub.flush();
                fosPub.close();
            } catch(FileNotFoundException e)
            {
                System.out.println("FileNotFoundException: "+e);
            } catch(IOException e)
            {
                System.out.println("IOException: "+e);
            }
            try
            {
                PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
                privKey.getEncoded());
                FileOutputStream fosPriv = new FileOutputStream(filePath+fileName+".priv");
                fosPriv.write(pkcs8EncodedKeySpec.getEncoded());
                fosPriv.flush();
                fosPriv.close();
              } catch(FileNotFoundException e)
            {
                System.out.println("FileNotFoundException: "+e);
            } catch(IOException e)
            {
                System.out.println("IOException: "+e);
            }

        }

        public SASP_RSA() throws NoSuchAlgorithmException, InvalidKeySpecException	{

	}
}
